package com.ruoyi.web.controller.basicInformation;

import java.util.*;

import com.ruoyi.common.utils.LocationUtils;
import com.ruoyi.system.basicInformation.bo.*;
import com.ruoyi.system.basicInformation.service.ITTerminalService;
import com.ruoyi.system.basicInformation.vo.TTerminalVo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.basicInformation.vo.TTerminalTrajectoryVo;
import com.ruoyi.system.basicInformation.service.ITTerminalTrajectoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 终端轨迹Controller
 *
 * @author ruoyi
 * @date 2021-05-20
 */
@Api(value = "终端轨迹控制器", tags = {"终端轨迹管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/basicInformation/trajectory")
public class TTerminalTrajectoryController extends BaseController {

    private final ITTerminalTrajectoryService iTTerminalTrajectoryService;

    private final ITTerminalService iTTerminalService;

    /**
     * 查询终端轨迹列表
     */
    @ApiOperation("查询终端轨迹列表")
    @GetMapping("/list")
    public TableDataInfo<TTerminalTrajectoryVo> list(TTerminalTrajectoryQueryBo bo) {

        List<TTerminalTrajectoryVo> list = iTTerminalTrajectoryService.queryList(bo);
        return getDataTable(list);
    }


    @ApiOperation("查询终端轨迹列表")
    @GetMapping("/getListByDate")
    public List<HashMap> getListByDate(TTerminalTrajectoryQueryBo bo){
        return iTTerminalTrajectoryService.getListByDate(bo);
    }
    /**
     * 导出终端轨迹列表
     */
    @ApiOperation("导出终端轨迹列表")
    @PreAuthorize("@ss.hasPermi('basicInformation:trajectory:export')")
    @Log(title = "终端轨迹", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<TTerminalTrajectoryVo> export(TTerminalTrajectoryQueryBo bo) {
        List<TTerminalTrajectoryVo> list = iTTerminalTrajectoryService.queryList(bo);
        ExcelUtil<TTerminalTrajectoryVo> util = new ExcelUtil<TTerminalTrajectoryVo>(TTerminalTrajectoryVo.class);
        return util.exportExcel(list, "终端轨迹");
    }

    /**
     * 获取终端轨迹详细信息
     */
    @ApiOperation("获取终端轨迹详细信息")
    @PreAuthorize("@ss.hasPermi('basicInformation:trajectory:query')")
    @GetMapping("/{id}")
    public AjaxResult<TTerminalTrajectoryVo> getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iTTerminalTrajectoryService.queryById(id));
    }

    /**
     * 新增终端轨迹
     */
    @ApiOperation("新增终端轨迹")
//    @PreAuthorize("@ss.hasPermi('basicInformation:trajectory:add')")
    @Log(title = "终端轨迹", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@RequestBody TTerminalTrajectoryAddBo bo) {
        return toAjax(iTTerminalTrajectoryService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改终端轨迹
     */
    @ApiOperation("修改终端轨迹")
    @PreAuthorize("@ss.hasPermi('basicInformation:trajectory:edit')")
    @Log(title = "终端轨迹", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult<Void> edit(@RequestBody TTerminalTrajectoryEditBo bo) {
        return toAjax(iTTerminalTrajectoryService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除终端轨迹
     */
    @ApiOperation("删除终端轨迹")
    @PreAuthorize("@ss.hasPermi('basicInformation:trajectory:remove')")
    @Log(title = "终端轨迹", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@PathVariable Long[] ids) {
        return toAjax(iTTerminalTrajectoryService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 删除终端轨迹
     */
    @ApiOperation("删除终端轨迹")
    @PreAuthorize("@ss.hasPermi('basicInformation:trajectory:remove')")
    @GetMapping("/rySynchronizationTerminalMileage/{id}")
    public AjaxResult<Double> rySynchronizationTerminalMileage(@PathVariable("id") Long id) {
        double mileage = 0f;
        try {
            TTerminalVo tTerminalVos = iTTerminalService.queryById(id);
            if (tTerminalVos != null) {
                Date s = getTodayStartTime();
                Date e = getTodayEndTime();
                TTerminalTrajectoryQueryBo bo1 = new TTerminalTrajectoryQueryBo();
                bo1.setTerminalType(tTerminalVos.getTerminalType());
                bo1.setTerminalId(tTerminalVos.getId());
                bo1.setBeginDate(s);
                bo1.setOverDate(e);
                List<TTerminalTrajectoryVo> tTerminalTrajectoryVos = iTTerminalTrajectoryService.queryList(bo1);
                if (tTerminalTrajectoryVos.size() > 1) {  //
                    for (Integer i = 0, j = 1; i < tTerminalTrajectoryVos.size() - 1; i++, j++) {
                        mileage += LocationUtils.getDistance(
                                tTerminalTrajectoryVos.get(i).getX().doubleValue(),
                                tTerminalTrajectoryVos.get(i).getY().doubleValue(),
                                tTerminalTrajectoryVos.get(j).getX().doubleValue(),
                                tTerminalTrajectoryVos.get(j).getY().doubleValue()
                        );
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success(mileage);
    }

    public static Date getTodayStartTime() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        return todayStart.getTime();
    }

    public static Date getTodayEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        return todayEnd.getTime();
    }
}
